﻿using ISaveReaddll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Reflection;

namespace FinalTestMachine.Motion.Products
{
    public class ProductNumberManager : LanguageControlBase.Wpf.BindableObject
    {
        public static readonly ProductNumberManager Cur = new ProductNumberManager();
        private ProductNumberManager()
        {
            Read();

        }
        string _path = System.IO.Path.Combine(System.Environment.CurrentDirectory, "AviewConfigs\\ProductNumsMsg.xml");
        private int[] NgCountList = new int[GlobalParams.WorkstationSum];

        int _okNums = 0;
        int _ngNums = 0;
        int _total = 0;

        int _socketAPinValue = 0;
        int _socketBPinValue = 0;
        int _socketCPinValue = 0;
        int _socketDPinValue = 0;
        int _socketEPinValue = 0;
        int _socketFPinValue = 0;

        #region 产品信息
        int _Site2Ng = 0;
        int _Site3Ng = 0;
        int _Site4Ng = 0;
        int _Site5Ng = 0;
        int _Site6Ng = 0;
        int _ngBox = 0;


        [SaveRemark]
        public int SocketAPinValue
        {
            get { return _socketAPinValue; }
            set
            {
                if (_socketAPinValue != value)
                {
                    _socketAPinValue = value;
                    onPropertyChanged(nameof(SocketAPinValue));

                    Save();
                }
            }
        }

        public double Yield => OKNums / (Total * 1.000) * 100;

        [SaveRemark]
        public int SocketBPinValue
        {
            get { return _socketBPinValue; }
            set
            {
                if (_socketBPinValue != value)
                {
                    _socketBPinValue = value;
                    Save();
                    onPropertyChanged(nameof(SocketBPinValue));
                }
            }
        }

        [SaveRemark]
        public int SocketCPinValue
        {
            get { return _socketCPinValue; }
            set
            {
                if (_socketCPinValue != value)
                {
                    _socketCPinValue = value;
                    Save();
                    onPropertyChanged(nameof(SocketCPinValue));
                }
            }
        }

        [SaveRemark]
        public int SocketDPinValue
        {
            get { return _socketDPinValue; }
            set
            {
                if (_socketDPinValue != value)
                {
                    _socketDPinValue = value;
                    Save();
                    onPropertyChanged(nameof(SocketDPinValue));
                }
            }
        }

        [SaveRemark]
        public int SocketEPinValue
        { 
            get { return _socketEPinValue; }
            set
            {
                if (_socketEPinValue != value)
                {
                    _socketEPinValue = value;
                    Save();
                    onPropertyChanged(nameof(SocketEPinValue));
                }
            }
        }

        [SaveRemark]
        public int SocketFPinValue
        {
            get { return _socketFPinValue; }
            set
            {
                if (_socketFPinValue != value)
                {
                    _socketFPinValue = value;
                    Save();
                    onPropertyChanged(nameof(SocketFPinValue));
                }
            }
        }


        /// <summary>
        /// OK总数
        /// </summary>
        [SaveRemark]
        public int OKNums
        {
            get { return _okNums; }
            set
            {
                if (_okNums != value)
                {
                    _okNums = value;
                    Save();
                    onPropertyChanged(nameof(OKNums));
                    Total++;
                }
            }
        }

        [SaveRemark]
        /// <summary>
        /// 总产量
        /// </summary>
        public int Total
        {
            get
            {
                return _total;
            }
            set
            {
                if(_total!=value)
                {
                    _total = value;
                    Save();
                    onPropertyChanged(nameof(Total));
                }
            }
        }

       


        [SaveRemark]
        /// <summary>
        /// NG总数
        /// </summary>
        public int NgNums
        {
            get
            {
                return _ngNums;
            }
            set
            {
                if (_ngNums != value)
                {
                    _ngNums = value;
                    Save();
                    onPropertyChanged(nameof(NgNums));
                    Total++;

                }
            }
        }

        [SaveRemark]
        public int NgBoxNums
        {
            get { return _ngBox; }
            set
            {
                if (_ngBox != value)
                {
                    _ngBox = value;
                    Save();
                    onPropertyChanged(nameof(NgBoxNums));
                }
            }
        }





        [SaveRemark]
        /// <summary>
        /// Site2Ng
        /// </summary>
        public int Site2NG
        {
            get { return _Site2Ng; }
            set
            {

                if (_Site2Ng != value)
                {
                    {
                        _Site2Ng = value;
                    }

                    Save();

                }
            }
        }

        [SaveRemark]
        /// <summary>
        /// Site3Ng
        /// </summary>
        public int Site3NG
        {
            get { return _Site3Ng; }
            set
            {
                if (_Site3Ng != value)
                {
                    {
                        _Site3Ng = value;
                    }

                    Save();
                }
            }
        }


        [SaveRemark]
        /// <summary>
        /// Site4Ng
        /// </summary>
        public int Site4NG
        {
            get { return _Site4Ng; }
            set
            {
                if (_Site4Ng != value)
                {
                    {
                        _Site4Ng = value;
                    }
                    Save();
                }
            }

        }

        [SaveRemark]
        public int Site5NG
        {
            get { return _Site5Ng; }
            set
            {
                if (_Site5Ng != value)
                {
                    _Site5Ng = value;
                    Save();
                }
            }

        }

        [SaveRemark]
        public int Site6NG
        {
            get { return _Site6Ng; }
            set
            {
                if (_Site6Ng != value)
                {
                    _Site6Ng = value;
                    Save();
                }
            }

        }

        #endregion
        public void Clear()
        {
            OKNums = 0;
            NgNums = 0;
            Total = 0;
            NgBoxNums = 0;

            _Site2Ng = 0;
            _Site3Ng = 0;
            _Site4Ng = 0;
            _Site5Ng = 0;
            _Site6Ng = 0;

            Save();
        }

        public void Save()
        {    
            XElement element = XElement.Load(_path);

            var collection = this.GetType().GetProperties();
            foreach (var item in collection)
            {
                SaveRemarkAttribute attribute = (SaveRemarkAttribute)item.GetCustomAttribute(typeof(SaveRemarkAttribute));
                if (attribute != null)
                {
                    XElement elementTemp = element.Element(item.Name);
                    if (elementTemp != null)
                    {
                        elementTemp.SetValue(item.GetValue(this));
                        element.Save(_path);
                    }
                    else
                    {
                        element.Add(new XElement(item.Name, item.GetValue(this)));
                        element.Save(_path);
                    }
                }
            }
 
        }
        public void Read()
        {
            try
            {
                if (!System.IO.File.Exists(_path))
                {
                    CreateXmlFile();
                }
                XElement element = XElement.Load(_path);
                var collection = this.GetType().GetProperties();
                foreach (var item in collection)
                {
                    SaveRemarkAttribute attribute = (SaveRemarkAttribute)item.GetCustomAttribute(typeof(SaveRemarkAttribute));
                    if (attribute != null)
                    {
                        XElement elementTemp = element.Element(item.Name);
                        if (elementTemp != null)
                        {
                            int num;
                            int.TryParse(elementTemp.Value, out num);
                            item.SetValue(this, num);
                        }
                        else
                        {
                            item.SetValue(this, 0);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.Message == "缺少根元素。")
                {
                    CreateXmlFile();
                }
                else
                {
                    System.Windows.MessageBox.Show($"{this}函数Read()引发异常:{ex.Message}");

                }
            }
           

        }



        private void CreateXmlFile()
        {
            try
            {
                XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement(this.GetType().ToString()),new XComment("生产计数保存"));
                var collection = this.GetType().GetProperties();
                foreach (var item in collection)
                {
                    SaveRemarkAttribute attribute = (SaveRemarkAttribute)item.GetCustomAttribute(typeof(SaveRemarkAttribute));
                    if (attribute != null)
                    {
                        doc.Root.Add(new XElement(item.Name, item.GetValue(this)));
                    }
                }

                doc.Save(_path);
            }
            catch (Exception ex)
            {
                throw;
            }
           
        }
        public static void SaveToXml(string filePath, string name, string value)
        {

        }

    }
}
